<?php
/**
 * Created by PhpStorm.
 * User: yzh
 * Date: 2019/5/10
 * Time: 16:06
 */

namespace app\api\model;


class Poster extends BaseModel
{

    protected $autoWriteTimestamp = true;

    public function user(){
        return $this->hasMany('PosterUser','poster_id','id');
    }

    public function material(){
        return $this->belongsTo('PosterMaterial','m_id','id');
    }

    public function good(){
        return $this->belongsTo('Product','goods_id','id')->field('id,name');
    }


    /**
     * 与image表一对一关系
     * @return \think\model\relation\BelongsTo
     */
    public function image(){
        return $this->belongsTo('Image','url','id');
    }


    /**
     * 创建海报数据
     * @param $data
     * @return Poster
     */
    public static function createPoster($data){
        return self::create($data);
    }

    /**
     * 获取名片
     * @param $wu_id
     * @param $m_id
     * @return array|false|\PDOStatement|string|\think\Model
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\ModelNotFoundException
     * @throws \think\exception\DbException
     */
    public static function getPoster($wu_id,$m_id){
        return self::where('wu_id','=',$wu_id)
                ->where('m_id','=',$m_id)->find();
    }

    /**
     * 更新名片信息
     * @param $id
     * @param $data
     * @return Poster
     */
    public static function updatePoster($id,$data){
        return self::where('id','=',$id)->update($data);
    }

    /**
     * 根据id获取海报信息
     * @param $id
     * @return Poster|null
     * @throws \think\exception\DbException
     */
    public static function getPosterById($id){
        return self::get($id);
    }

    /**
     * 浏览次数递增
     * @param $id
     * @return int|true
     * @throws \think\Exception
     */
    public static function addBrowerNum($id){
        return self::where('id','=',$id)->setInc('brower_num');
    }

    /**
     * 获取名片列表
     * @param $wu_id
     * @param $size
     * @param $page
     * @return \think\Paginator
     * @throws \think\exception\DbException
     *
     */
    public static function getPosterList($wu_id,$size,$page){
        $data = self::where('wu_id','=',$wu_id)
                ->field('id,wu_id,m_id,url,update_time')
                ->order('create_time desc')
                ->withCount(['user'])
                ->with(['image'])
               ->paginate($size,false,['page'=>$page]);
        return $data;
    }

    /**
     * 获取用户数量
     * @param $wu_id
     * @return false|\PDOStatement|string|\think\Collection
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\ModelNotFoundException
     * @throws \think\exception\DbException
     */
    public static function  getAllUser($wu_id){
        return self::where('wu_id','=',$wu_id)->field('id')->withCount(['user'])->select();
    }

    /**
     * 获取用户
     * @param $id
     * @return false|\PDOStatement|string|\think\Collection
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\ModelNotFoundException
     * @throws \think\exception\DbException
     */
    public static function getUser($id){
        return self::where('id','=',$id)
            ->with(['image'])
            ->with(['user'=>function($query){
                $query->with(['users']);
            }])
            ->with(['good'])
            ->field('id,m_id,wu_id,scene,page,word,eject,url,goods_id,brower_num,update_time')
            ->select();
    }



}